<?php

/***************************************************************\
* FILE:		sys/pages/poll.inc
* SECTION:	pages > items
* FUNCTION:	display a poll
* USES:	
* INCLUDES:	/sys/itemfunctions.inc	
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/itemfunctions.inc');

/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	item_page_inc_check_access();
}

/**
 * appropriate page action interpretations
 */
function page_inc_execute_action($action) {
	switch($action) {
		case "change_vote":
		case "show_results":
			break;
		default:
			item_page_inc_execute_action($action, 'poll');
			break;
	}
}

/**
 * execute page logic
 */
function page_inc_execute() {
	global $PAGE, $has_voted;
	$CONTENT = array();

	$where = "ITEM.item_id = {$PAGE['id']} AND \n\t";
	$where .= "ITEM.item_id = POLL.item_id AND \n\t";
	$where .= "ITEM.author = M1.user_id AND \n\t";
	$where .= "ITEM.editor = M2.user_id";

	$sql = db_query_select(array(array('*'),array('body'),array(),"M1.nick AS nick_author, M2.nick AS nick_editor"), array('ITEM','POLL','MEMBER AS M1, MEMBER AS M2', 'DUMMY'), $where);
	$sqlresult = db_do_query($sql);

	if (db_num_rows($sqlresult) !=1)
		errorpage(404);

	$CONTENT['poll'] = db_fetch_array($sqlresult);

	if ($CONTENT['poll']['deleted'])
		errorpage(404);
	
	$CONTENT['title'] = $CONTENT['poll']['title'];
	$CONTENT['owner'] = $CONTENT['poll']['author'];
	
	$ipstring = $_SERVER["REMOTE_ADDR"]." - ".$_SERVER["HTTP_X_FORWARDED_FOR"];
	$anonymous = md5("{$PAGE['id']} {$_SESSION['user_id']} $ipstring");
	
	if ($_COOKIE[$CONFIG['cookie_name'] . 'poll' . $PAGE['id']])
		$where = "anonymous_id = '{$_COOKIE['mdgpoll' . $PAGE['id']]}'";
	else
		$where = "anonymous_id = '$anonymous'";
	if ($_SESSION['user_id'])
		$where = "(($where) OR (user_id = {$_SESSION['user_id']}))";
	$where = "$where AND item_id = {$PAGE['id']}";
	$sql = db_query_select(array(array('*')), array("POLL_VOTE"), $where);
	$sqlresult = db_do_query($sql);
	if (db_num_rows($sqlresult) > 0) {
		$has_voted = 1;
		$CONTENT['poll']['uservote'] = db_fetch_array($sqlresult);
		if ($_GET['action'] != "change_vote")
			add_action("change_vote", "url-action", "change_vote");
	} else
		if ($_GET['action'] != "show_poll_results")
			add_action("show_poll_results", "url-action", "show_results");
		
	if ($_POST['submitted'] and $_POST['voting']) {
	
		check_access("VOTE");
		
		if ($has_voted)
			$sql = db_query_update(array('answer_id'), array($_POST['choice']), "POLL_VOTE", "vote_id = {$CONTENT['poll']['uservote']['vote_id']}");
		else
			$sql = db_query_insert(array('item_id', 'answer_id', 'user_id', 'anonymous_id'), array($PAGE['id'], $_POST['choice'], $_SESSION['user_id'] ? $_SESSION['user_id'] : "NULL", db_sqlize($anonymous)), "POLL_VOTE", "vote_id = {$CONTENT['poll']['uservote']['vote_id']}");
		
		db_do_query($sql);
		
		setcookie($_COOKIE[$CONFIG['cookie_name'] . 'poll' . $PAGE['id']], $anonymous, time() + 86400000, "/");
			
		$has_voted = 1;
		add_alert("Uw stem is geregistreerd");
	}

	$sql = db_query_select(array(array(), "POLL_ANSWER.answer_id, POLL_ANSWER.answer, COUNT(vote_id) AS votes"), array("POLL_ANSWER LEFT JOIN POLL_VOTE ON POLL_ANSWER.answer_id = POLL_VOTE.answer_id", "DUMMY"), "POLL_ANSWER.item_id = {$PAGE['id']}", "", "", "POLL_ANSWER.answer_id");
	$sqlresult = db_do_query($sql);
	$CONTENT['poll']['answers'] = array();
	while ($a = db_fetch_array($sqlresult))
		array_unshift($CONTENT['poll']['answers'], $a);
	
	if ((($_GET['action'] == 'change_vote') or !$has_voted) and has_access("VOTE")) {
		
		$as = $CONTENT['poll']['answers'];
		$keys = array();
		$vals = array();
		while ($a = array_shift($as)) {
			array_unshift($keys, $a['answer_id']);
			array_unshift($vals, $a['answer']);
		}
		$inputs = array(form_input_hidden("voting", 1), form_input_radio("Stem", "choice", $keys, $vals, $CONTENT['poll']['uservote']['answer_id']));
		$CONTENT['poll']['form'] = form_create("voteform", $inputs, "{$PAGE['dirpath']}/{$PAGE['id']}.poll");		
	}
		
	$PAGE['content'] = $CONTENT;
	
	item_page_inc_execute('poll');
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE, $has_voted;
	
	item_page_inc_dress_before();

	if ($has_voted or ($_GET['action'] == 'show_results') or !has_access("VOTE"))
		$answers = $PAGE['content']['poll']['answers'];
	
	if ((!$has_voted or ($_GET['action'] == 'change_vote')) and has_access("VOTE"))
		$form = $PAGE['content']['poll']['form'];

	print_poll($PAGE['content']['poll'], $answers, $form);
		
	item_page_inc_dress_after();
}

?>
